<?php
namespace Api\Controller;

use Think\Controller;

/**
 * 扩展接口
 *
 * @author ryan
 *        
 */
class ExtendsController extends Controller
{

    public function regions()
    {
        $regions = $this->getRegion();
        $this->ajaxReturn(array(
            'status' => 0,
            'data' => $regions
        ));
    }
    
    public function demandConfig() {
        $status = C('DEMAND_CONFIG.STATUS');
        $type = C('DEMAND_CONFIG.TYPE');
        $source = C('DEMAND_CONFIG.SOURCE');
        $bank_nature = C('DEMAND_CONFIG.BANKNATURE');
        $data = array();
        $data['status'] = $status;
        $data['type'] = $type;
        $data['source'] = $source;
        $data['nature'] = $bank_nature;
        $this->ajaxReturn(array(
            'status' => 0,
            'data' => $data
        ));
    }

    private function getRegion($pid = 0, $level = 2)
    {
        $condition = array();
        $condition['pid'] = $pid;
        $condition['level'] = array(
            'ELT',
            $level
        );
        $regions = D('Regions')->where($condition)
            ->order('id asc')
            ->select();
        foreach ($regions as $key => $region) {
            $children = $this->getRegion($region['id'], $level);
            if ($level > $region['level']) {
                $region['children'] = $children;
            }
            $regions[$key] = $region;
        }
        return $regions;
    }
}

